import axios from "axios";

// let useUrl = 'http://192.168.31.46:6003'
let useUrl = "https://test.easytouch.top";
// let useUrl = "https://test.easytouch.top/megadev";
// let useUrl = "https://test.easytouch.top/local_mega/yzb";
// let useUrl = "https://test.easytouch.top/local_mega/wgj";
// let useUrl = "https://test.easytouch.top/local_mega/sj";
//  let useUrl = 'https://pay.easytouch.top'

axios.defaults.baseURL = useUrl
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'

// console.log("VITE_APP_BASE_API=", import.meta.env.VITE_APP_BASE_API);

// let useUrl = import.meta.env.VITE_APP_BASE_API;
let url_query = window.location.href;
if (
  url_query.indexOf("192.168.31.88") > -1 &&
  url_query.indexOf("name=") > -1
) {
  let name_query = window.location.href.split("name=")[1].split("#/")[0];
  console.log("name_query:" + name_query);
  switch (name_query) {
    case "yzb":
      useUrl = "https://test.easytouch.top/local_mega/yzb";
      break;
    case "chn":
      useUrl = "https://test.easytouch.top/megadev";
      break;
    case "sj":
      useUrl = "https://test.easytouch.top/local_mega/sj";
      break;
    case "wgj":
      useUrl = "https://test.easytouch.top/local_mega/wgj";
      break;
    case "pay":
      useUrl = "https://pay.easytouch.top";
      break;
    default:
      useUrl = "https://test.easytouch.top";
      break;
  }
}

//使用create方法创建axios实例
const Axios = axios.create({
  timeout: 10000, // 请求超时时间
  baseURL: useUrl,
  method: "POST",
});

// 添加响应拦截器
Axios.interceptors.response.use((data) => {
  let href = window.location.href.split("#/")[0];
  if (data.status === 200 && data.data.code === 401001) {
    window.location.replace(`${href}`);
    return;
  }
  return data;
});

export default Axios;
